package com.ebt.m.policy.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.policy.ActBrandProductsAll;
import com.ebt.m.policy.bean.BrandProductTitle;
import com.ebt.m.utils.imageloader.ImageLoader;
import com.ebt.m.utils.imageloader.ImageLoaderUtil;


/**
 * @author Damon.Han
 */
public class BrandsProductViewTitle extends IView {

    ImageView img;
    TextView name;
    TextView count;

    public BrandsProductViewTitle(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View view = inflate(context, R.layout.view_brand_product_title, this);
        img = (ImageView) view.findViewById(R.id.img);
        name = (TextView) view.findViewById(R.id.name);
        count = (TextView) view.findViewById(R.id.count);
    }

    public BrandsProductViewTitle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BrandsProductViewTitle(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... args) {
        BrandProductTitle title = (BrandProductTitle) args[0];
        ImageLoaderUtil.getInstance().loadImage(getContext(), new ImageLoader.Builder().url(title.getMinLogo())
                .placeHolder((R.drawable.ic_company_default)).circle().thumb(0.3f).imgView(img).build());
        name.setText(title.getName());
        count.setVisibility(GONE);
        if (title.getProductCount() > 2) {
            count.setVisibility(VISIBLE);
            count.setText("更多(" + title.getProductCount() + ")");
        }
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", title);
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(getContext(), ActBrandProductsAll.class);
                getContext().startActivity(intent);

            }
        });
    }

}
